<?php
class NewsAdminCatsEditForm extends Form
{
	private $row=false;
	
	function __construct()
	{				
		Form::Form('NewsAdminCatsEditForm');
		
		$catid=Url::get('id',0);
		
		if($catid)
		{
			$this->row = DB::select('news_categories',$catid);
		}
		
		if(!$this->row)
			Url::redirect_current();
	}
	
	function on_submit()
	{
		$name			=Url::get('name');
		$public			=Url::get('public',1);
		$position		=Url::get('position','int',0);
		$keywords		=Url::get('keywords');
		$description	=Url::get('description');
		
		if($public != 1)
			$public=0;
			
		$this->checkFormInput("Tên danh mục tin",'name',$name,'str',true,'',2,100);
		$this->checkFormInput("Vị trí",'position',$position,'int',true,'',1, DB::count("news_categories"));
			
		if(!$this->errNum)
		{
			DB::update('news_categories', array(
												'name'			=>$name,
												'keywords'		=>$keywords,
												'description'	=>$description,
												'position'		=>$position,
												'public'		=>$public,
												'user_modify'	=>User::user_name(),
												'time_modify'	=>TIME_NOW,
												),
												'id='.$this->row['id']);
			
			if($this->row['position'] != $position)
			{
				DB::query("UPDATE news_categories SET position=".$this->row['position']." WHERE id!=".$this->row['id']." AND position=".$position);
			}
				
			AZLib::getNewsCats(1);
			Url::redirect_current();
		}
	}
	
	function draw()
	{
		global $display;
		
		$this->beginForm();
		
		$display->add('url_add',Url::build_current(array('cmd'=>'add')));
		
		$current_path = "<a href='".URL::build('news_list',array('news_catid'=> $this->row['id'], 'azname' =>AZLib::safe_title($this->row['name'])))."'>".$this->row['name']."</a>";
		
		$display->add('current_cat',$current_path);
		
		$display->add('name',Url::get('name',$this->row['name']));
		
		$display->add('position_option',AZLib::getOptionNum(1,DB::count("news_categories"),Url::get('position',$this->row['position'])));
		
		$display->add('public',Url::get('public', ($this->row['public'] ? 1:2)));
		
		$display->add('keywords', Url::get('keywords',$this->row['keywords']));
		$display->add('description', Url::get('description',$this->row['description']));
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$display->output("NewsAdminCatsEdit");
		
		$this->endForm();
	}
}
?>